package com.hanhai.zrb.backend.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.IntentionMessageService;
import com.hanhai.zrb.backend.admin.model.BackendUser;
import com.hanhai.zrb.backend.util.BackendLoginUtil;
import com.hanhai.zrb.model.intention.IntentionMessage;
import com.hanhai.zrb.model.intention.dto.IntentionMessageDto;

@Controller
@RequestMapping("/intentionMessage")
public class IntentionMessageController extends BaseController {

	@Resource
	private IntentionMessageService intentionMessageService;

	@RequestMapping("/index")
	public String index() {
		return "intentionMessage/index";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public String list(IntentionMessageDto intentionMessageDto,Integer page,Integer rows ) {
		Map<String, Object> map = Maps.newHashMap();
		PageInfo pageInfo =intentionMessageService.findIntentionMessages(intentionMessageDto, page, rows);
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return JSON.toJSONString(map);
	}
	
	@RequestMapping(value="/edit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String edit(IntentionMessage intentionMessage){
		Map<String, Object> map = Maps.newHashMap();
		if(intentionMessageService.updateIntentionMessage(intentionMessage)){
			map.put("success", true);
			map.put("msg", "添加成功!");
		}else{
			map.put("success", false);
			map.put("msg", "添加失败!");
		}
		
		return JSON.toJSONString(map);
	}
	
	@RequestMapping(value="/multiHandle",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String multiHandle(String ids){
		Map<String, Object> map = Maps.newHashMap();
		BackendUser backendUser = BackendLoginUtil.getCurrentUser();
		String[] str = StringUtils.split(ids, ",");
		List<IntentionMessage> list = Lists.newArrayList();
		for (String id : str) {
			IntentionMessage intentionMessage = new IntentionMessage();
			intentionMessage.setId(Long.valueOf(id));
			intentionMessage.setHandlePerson(backendUser.getUserName());
			intentionMessage.setHandleTime(new Date());
			intentionMessage.setStatus(1);
			list.add(intentionMessage);
		}
		if (intentionMessageService.batchUpdateIntentionMessage(list)) {
			map.put("success", true);
			map.put("msg", "处理成功!");
		}else{
			map.put("success", false);
			map.put("msg", "处理失败!");
		}
		return JSON.toJSONString(map);
	}
	
	@InitBinder
	@Override
	protected void initBinder(WebDataBinder binder) throws ServletException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);

		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(dateFormat, true));

		CustomCollectionEditor collectionEditor = new CustomCollectionEditor(List.class, true);

		binder.registerCustomEditor(List.class, collectionEditor);
	}
}
